package ngames.com.sushitv.base;

import android.app.Activity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import ngames.com.sushitv.R;
import ngames.com.sushitv.tools.ScreenUtil;
import ngames.com.sushitv.view.DividerItemDecoration;

/**
 * Created by Administrator on 2016/3/5.
 */
public abstract class BaseListAction extends BaseAction {
    //protected SwipeRefreshLayout mSwipeRefreshLayout;
    protected RecyclerView mRecyclerView;
    //protected LinearLayoutManager mLinearLayoutManager;
    protected boolean mIsLoadingMore = false;

    public BaseListAction(Activity activity, View baseView) {
        super(activity, baseView);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //initRecyclerView();
    }

    /*protected void initRecyclerView() {
        mSwipeRefreshLayout = (SwipeRefreshLayout) mBaseView.findViewById(R.id.refresh_layout);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onRefreshData();
            }
        });
        mRecyclerView = (RecyclerView) mBaseView.findViewById(R.id.recycler_view_list);
        mLinearLayoutManager = new LinearLayoutManager(mActivity);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int firstItemVisible = mLinearLayoutManager.findFirstVisibleItemPosition();
                int lastVisibleItem = mLinearLayoutManager.findLastVisibleItemPosition();
                int totalItemCount = mRecyclerView.getAdapter().getItemCount();
//                LogUtil.d("TagsFragment", "firstItemVisible = " + firstItemVisible + ", lastVisibleItem = " + lastVisibleItem + ",totalItemCount = " + totalItemCount + ",dy = " + dy);
                if (lastVisibleItem > totalItemCount - 2 && dy > 0) {
                    if (!mIsLoadingMore) {
                        mIsLoadingMore = true;
                        loadMore();
                    }
                }
            }
        });
    }*/

    protected DividerItemDecoration setListDividerHeight(int color, int height) {
        DividerItemDecoration itemDecoration = new DividerItemDecoration(LinearLayoutManager.VERTICAL);
        itemDecoration.setColor(color);
        itemDecoration.setSize(ScreenUtil.dip2px(mActivity, height));
        mRecyclerView.addItemDecoration(itemDecoration);
        return itemDecoration;
    }

    /**
     * 下拉刷新。结束之后需要设置mSwipeRefreshLayout.setRefreshing(false);
     */
    public abstract void onRefreshData();

    /**
     * 加载更多。结束之后设置mIsLoadingMore = false;
     */
    public abstract void loadMore();
}
